﻿/// <reference path="zhx.js" />
//cutImg
$(function() {
    $("input[cutImg]").each(function() {
        $(this).hide();
        var _img = "";
        if ($.trim($(this).val()) != "") {
            _img = "<img style=\"border:1px solid #7F9DB9;\" src=\"" + $.trim($(this).val()) + "\" />";
        }
        if ($.trim($(this).attr("cutImg").toString()) == "" || $.trim($(this).attr("cutImg").toString()) == "{}") {
            $(this).before("<div id=\"div" + $(this).attr("id") + "\" style=\" margin-bottom:5px;\" >" + _img + "</div><a class=\"btA\" href=\"javascript:cutImg('','','" + $(this).attr("id") + "','');\">图片上传通道</a> ");
        }
        else {
            $(this).before("<div id=\"div" + $(this).attr("id") + "\" style=\" margin-bottom:5px;\" >" + _img + "</div><a class=\"btA\" href=\"javascript:cutImg('" + $.trim($(this).attr("cutImg")).split(',')[0] + "','" + $.trim($(this).attr("cutImg")).split(',')[1] + "','" + $(this).attr("id") + "','');\">图片上传通道</a> ");
        }
    });
    $("input[minImg]").each(function() {
        var _minimg = $.trim($(this).attr("minImg")).split('|');
        $(_minimg).each(function() {
            var _width = this.split(',')[0];
            var _height = this.split(',')[1];
            var _select = this.split(',')[2];
            $("#" + _select).hide();
            var _img = "";
            if ($.trim($("#" + _select).val()) != "") {
                _img = "<img style=\"border:1px solid #7F9DB9;\" src=\"" + $.trim($("#" + _select).val()) + "\" />";
            } else {
                _img = "无缩略图";
            }
            $("#" + _select).before("<div id=\"div" + _select + "\" >" + _img + "</div><div>(自动生成)<div>");
        });
    });
});
function cutImg(imgWidth, imgHeight, txtSelect, returnIfarme) {
    var _toPath = "?1=1";
    if (returnIfarme != "") {
        _toPath = "?p=" + returnIfarme;
    }
    showBox("400", '<div style="line-height:30px; padding:5px 0px 10px 0px;"><p>1、选择要上传的文件：</p><span id="upload1"  style="color:#900;">上传通道加载中...</span><p>仅支持JPG、GIF、PNG、BMP图像文件</p><div id="cutImgInfo"></div></div>');
    $("#upload1").uploadify({
        'uploader': toolsPath() + 'flash/uploadify2.swf',
        'cancelImg': toolsPath() + 'images/cancel.png',
        'buttonImg': toolsPath() + 'images/selectFile.png',
        'width': 74,
        'height': 12,
        'script': '/tools/upload/img.aspx' + _toPath,
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png;*bmp',
        'fileDesc': '*.jpg;*.jpeg;*.gif;*.png;*bmp',
        'sizeLimit': 1073741824, //1G
        'simUploadLimit': 10,
        'multi': false,
        'auto': true,
        'onSelect': function(a, b, c) { },
        'onComplete': function(a, b, c, d, e) {
            $(".uploadifyQueueItem").remove();
            var _cutcontent1 = "<p style=\"color:#900;\">图片加载中...</p>";
            $("#cutImgInfo").html(_cutcontent1);
            showCutContent(c.filePath, imgWidth, imgHeight, txtSelect, returnIfarme);
        }
    });
}
function showCutContent(src, imgWidth, imgHeight, txtSelect, returnIfarme) {
    var imgID = new Image();
    imgID.src = src + "?" + Math.random();
    imgID.onload = function () {
        var _zjsc = 'toTpSrc(\'' + src + '\',\'' + txtSelect + '\');';
        if (returnIfarme != "") {
            _zjsc = 'toTpSrc2(\'' + src + '\',\'' + txtSelect + '\',\'' + returnIfarme + '\');';
        }
        var _cutcontent2 = '<p>2、图片剪裁：</p><p>如果是GIF动画，请点击<span onclick="' + _zjsc + '" style="cursor:pointer; color:#900; text-decoration:underline; font-weight:bolder;">直接上传</span></p><p><span onclick="toRL(\'' + src + '\',\'' + imgWidth + '\',\'' + imgHeight + '\',\'' + txtSelect + '\',\'' + returnIfarme + '\');" style=" cursor:pointer; color:#900; text-decoration:underline; font-weight:bolder;">逆时针旋转</span>&nbsp;&nbsp;<span onclick="toRR(\'' + src + '\',\'' + imgWidth + '\',\'' + imgHeight + '\',\'' + txtSelect + '\',\'' + returnIfarme + '\');" style=" cursor:pointer; color:#900; text-decoration:underline; font-weight:bolder;">顺时针旋转</span></p><p>宽度：<span id="j_w">0</span>，高度：<span id="j_h">0</span>&nbsp;&nbsp;<span id="tjtp" style="display:none; cursor:pointer; color:#900; text-decoration:underline; font-weight:bolder;">点击提交裁剪图片</span></p><img style=\"border:1px solid #7F9DB9;\" id="cutImg1" src="' + imgID.src + '" /><p><a id="preview2" href="javascript:;" onclick="if($(this).html()==\'点击显示裁剪效果图\'){$(this).html(\'点击隐藏裁剪效果图\');$(\'#preview\').show();}else{$(this).html(\'点击显示裁剪效果图\');$(\'#preview\').hide();}">点击显示裁剪效果图</a><p><div style="width:0px;height:0px;overflow:hidden;display:none;" id="preview"><img style=\"border:1px solid #7F9DB9;\" src="' + imgID.src + '" /></div>';
        $("#cutImgInfo").html(_cutcontent2);
        if ($.trim(imgWidth) != "") {
            if (parseInt($.trim(imgWidth)) > 398) {
                var w1 = parseInt($.trim(imgWidth));
                $("#box").width(parseInt(w1) + 2);
            }
            else {
                $("#box").width(400);
            }
        }
        else {
            $("#box").width(400);
        }
        updateBox();
        if ($.trim(imgWidth) != "" && $.trim(imgHeight) != "") {
            $("#cutImg1").Jcrop({
                onChange: function (c) {
                    $("#j_w").html(c.w);
                    $("#j_h").html(c.h);
                    if (parseInt(c.w) > 0 && parseInt(c.h) > 0) {
                        $("#preview").width(parseInt(imgWidth));
                        $("#preview").height(parseInt(imgHeight));
                        var rx = parseInt(imgWidth) / c.w;
                        var ry = parseInt(imgHeight) / c.h;
                        var iw = parseInt($("#cutImg1").width());
                        var ih = parseInt($("#cutImg1").height());
                        $('#preview img').css({
                            width: Math.round(rx * iw) + 'px',
                            height: Math.round(ry * ih) + 'px',
                            marginLeft: '-' + Math.round(rx * c.x) + 'px',
                            marginTop: '-' + Math.round(rx * c.y) + 'px'
                        });
                        $("#tjtp").show();
                        $("#tjtp").unbind("click");
                        $("#tjtp").click(function () {
                            tjtp(src, c.x, c.y, c.w, c.h, imgWidth, imgHeight, txtSelect, returnIfarme);
                        });
                    }
                    else {
                        $("#preview").width(0);
                        $("#preview").height(0);
                        $("#tjtp").hide();
                    }
                },
                aspectRatio: parseInt(imgWidth) / parseInt(imgHeight),
                boxWidth: parseInt($.trim(imgWidth)) > 398 ? parseInt($.trim(imgWidth)) : 398
            });
        }
        else {
            $("#cutImg1").Jcrop({
                onChange: function (c) {
                    $("#j_w").html(c.w);
                    $("#j_h").html(c.h);
                    if (parseInt(c.w) > 0 && parseInt(c.h) > 0) {
                        $("#preview").width(parseInt(c.w));
                        $("#preview").height(parseInt(c.h));
                        $('#preview img').css({
                            width: Math.round($("#cutImg1").width()) + 'px',
                            height: Math.round($("#cutImg1").height()) + 'px',
                            marginLeft: '-' + Math.round(c.x) + 'px',
                            marginTop: '-' + Math.round(c.y) + 'px'
                        });
                        $("#tjtp").show();
                        $("#tjtp").unbind("click");
                        $("#tjtp").click(function () {
                            tjtp(src, c.x, c.y, c.w, c.h, "", "", txtSelect, returnIfarme);
                        });
                        var _w2 = parseInt(c.w) > 398 ? parseInt(c.w) : 398;
//                        if ($("#preview2").html() == "点击隐藏裁剪效果图") {
//                            $("#box").width(_w2 + 2);
//                        }
                        //updateBox();
                    }
                    else {
                        $("#preview").width(0);
                        $("#preview").height(0);
                        $("#tjtp").hide();
                    }
                },
                onSelect: function (c) {
                    //updateBox();
                },
                boxWidth: 398
            });
        }
    };
}
function toRL(src, imgWidth, imgHeight, txtSelect, returnIfarme) {
    $(".uploadifyQueueItem").remove();
    var _cutcontent1 = "<p style=\"color:#900;\">图片加载中...</p>";
    $("#cutImgInfo").html(_cutcontent1);
    $.get("/tools/cutimg.aspx?t=r&a=-90&s=" + src + "&" + Math.random(), function (data) {
        if ($.trim(data) != "success") {
            alert(data);
        }
        else {
            showCutContent(src, imgWidth, imgHeight, txtSelect, returnIfarme);
        }
    });
}
function toRR(src, imgWidth, imgHeight, txtSelect, returnIfarme) {
    $(".uploadifyQueueItem").remove();
    var _cutcontent1 = "<p style=\"color:#900;\">图片加载中...</p>";
    $("#cutImgInfo").html(_cutcontent1);
    $.get("/tools/cutimg.aspx?t=r&a=90&s=" + src + "&" + Math.random(), function(data) {
        if ($.trim(data) != "success") {
            alert(data);
        }
        else {
            showCutContent(src, imgWidth, imgHeight, txtSelect, returnIfarme);
        }
    });
}
function tjtp(src, x, y, w, h, w2, h2, txtSelect, returnIfarme) {
    if (w2 != "" && h2 != "") {
        $.get("/tools/cutimg.aspx?t=g&s=" + src + "&x=" + x + "&y=" + y + "&w=" + w + "&h=" + h + "&w2=" + w2 + "&h2=" + h2 + "&" + Math.random(), function (data) {
            if ($.trim(data) != "success") {
                alert(data);
            }
            else {
                if (returnIfarme == "") {
                    toTpSrc(src, txtSelect);
                } else {
                    toTpSrc2(src, txtSelect, returnIfarme);
                }
            }
        });
    } else {
        $.get("/tools/cutimg.aspx?t=g&s=" + src + "&x=" + x + "&y=" + y + "&w=" + w + "&h=" + h + "&" + Math.random(), function (data) {
        if ($.trim(data) != "success") {
            alert(data);
        }
        else {
            if (returnIfarme == "edit") {
                toTpSrc2(src);
            } else {
                toTpSrc(src, txtSelect);
            }
        }
    });
    }
}
function toTpSrc(src, txtSelect) {
    closeBox();
    $("#" + txtSelect).val(src);
    $("#div" + txtSelect).html("<img style=\"border:1px solid #7F9DB9;\" src=\"" + src + "\" />");
    $("#" + txtSelect + "[minImg]").each(function () {
        var _this = this;
        var _minimg = $.trim($(this).attr("minImg")).split('|');
        $(_minimg).each(function () {
            var _width = this.split(',')[0];
            var _height = this.split(',')[1];
            var _select = this.split(',')[2];
            if ($.trim($(_this).val()) == "") {
                $("#div" + _select).html("无缩略图");
                $("#" + _select).val("");
            } else {
                $.ajax({
                    url: "/tools/cutimg.aspx?t=t&s=" + $.trim($(_this).val()) + "&w=" + _width + "&h=" + _height + "&" + Math.random(),
                    async: false,
                    success: function (data) {
                        $("#div" + _select).html("<img style=\"border:1px solid #7F9DB9;\" src=\"" + data + "\" />");
                        $("#" + _select).val(data);
                    }
                });
            }
        });
    });
}
function toTpSrc2(src) {
    
    //closeBox();
    //$("#xheImgSrc").val(src);
    //$(".xheModalClose").trigger("click");
    //$(".xheText").val(src);
    closeBox();
    $(".xheModalClose").trigger("click");
    $(".xheText").val(src);
}